package dao;

import tool.Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AddUserDao {
    Util util = new Util();
    Connection con = null;

    public void addUser(String username1, String username2) {
        con = util.getConnection();
        String sql = "insert into friendships(username1, username2) VALUES (?,?)";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, username1);
            preparedStatement.setString(2, username2);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public boolean checkStatus(String username1, String username2) {
        con = util.getConnection();
        String sql = "select * from friendships where ((username1=? and username2=?) or (username1=? and username2=?)) and status=0";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, username1);
            preparedStatement.setString(2, username2);
            preparedStatement.setString(3, username2);
            preparedStatement.setString(4, username1);
            ResultSet resultSet = preparedStatement.executeQuery();
            return !resultSet.next();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean checkStatus1(String username1, String username2) {
        con = util.getConnection();
        String sql = "select * from friendships where ((username1=? and username2=?) or (username1=? and username2=?))";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, username1);
            preparedStatement.setString(2, username2);
            preparedStatement.setString(3, username2);
            preparedStatement.setString(4, username1);
            ResultSet resultSet = preparedStatement.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean checkUser(String username) {
        con = util.getConnection();
        String sql = "select * from user where name=?";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void updateUser(String username1, String username2) {
        con = util.getConnection();
        String sql = "update friendships set status=0 where ((username1=? and username2=?)or (username1=? and username2=?))";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, username1);
            preparedStatement.setString(2, username2);
            preparedStatement.setString(3, username2);
            preparedStatement.setString(4, username1);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}